package cn.stylefeng.guns.modular.hx.storage.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.util.DateUtil;
import cn.stylefeng.guns.modular.basic.service.BasicCustomerService;
import cn.stylefeng.guns.modular.basic.service.BasicWarehouseService;
import cn.stylefeng.guns.modular.hx.storage.entity.HxStorageOutDetailed;
import cn.stylefeng.guns.modular.hx.storage.model.params.HxStorageOutDetailedParam;
import cn.stylefeng.guns.modular.hx.storage.model.params.HxStorageOutParam;
import cn.stylefeng.guns.modular.hx.storage.model.params.HxStoragePutDetailedParam;
import cn.stylefeng.guns.modular.hx.storage.model.result.HxStorageOutDetailedResult;
import cn.stylefeng.guns.modular.hx.storage.model.result.HxStoragePutDetailedResult;
import cn.stylefeng.guns.modular.hx.storage.service.HxStorageOutDetailedService;
import cn.stylefeng.guns.modular.hx.storage.service.HxStoragePutDetailedService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.util.ToolUtil;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 拣货单明细表控制器
 *
 * @author zhucx
 * @Date 2023-09-05 16:05:54
 */
@Controller
@RequestMapping("/hxStorageOutDetailed")
public class HxStorageOutDetailedController extends BaseController {

    private String PREFIX = "/hx/storage/hxStorageOutDetailed";

    @Autowired
    private HxStorageOutDetailedService hxStorageOutDetailedService;

    @Autowired
    private UserService userService;

    @Autowired
    private BasicCustomerService basicCustomerService;

    @Autowired
    private BasicWarehouseService basicWarehouseService;

    /**
     * 跳转到主页面
     *
     * @author zhucx
     * @Date 2023-09-05
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/hxStorageOutDetailed.html";
    }

    /**
     * 跳转到出库记录页面
     *
     * @author zhucx
     * @Date 2023-09-05
     */
    @RequestMapping("outjilu")
    public String outjilu(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	//查询可选择仓库
    	model.addAttribute("basicWarehouseList", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
    	//查询可选择客户
    	model.addAttribute("customerlist", basicCustomerService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/hxStorageOutDetailed_jilu.html";
    }

    /**
     * 新增页面
     *
     * @author zhucx
     * @Date 2023-09-05
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/hxStorageOutDetailed_add.html";
    }

    /**
     * 编辑页面
     *
     * @author zhucx
     * @Date 2023-09-05
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/hxStorageOutDetailed_edit.html";
    }

    /**
     * 新增接口
     *
     * @author zhucx
     * @Date 2023-09-05
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(HxStorageOutDetailedParam hxStorageOutDetailedParam) {
        this.hxStorageOutDetailedService.add(hxStorageOutDetailedParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author zhucx
     * @Date 2023-09-05
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(HxStorageOutDetailedParam hxStorageOutDetailedParam) {
        this.hxStorageOutDetailedService.update(hxStorageOutDetailedParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author zhucx
     * @Date 2023-09-05
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(HxStorageOutDetailedParam hxStorageOutDetailedParam) {
        this.hxStorageOutDetailedService.delete(hxStorageOutDetailedParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author zhucx
     * @Date 2023-09-05
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(HxStorageOutDetailedParam hxStorageOutDetailedParam) {
        HxStorageOutDetailed detail = this.hxStorageOutDetailedService.getById(hxStorageOutDetailedParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询出库记录列表
     *
     * @author zhucx
     * @Date 2023-09-05
     */
    @ResponseBody
    @RequestMapping("/jilulist")
    public LayuiPageInfo jilulist(HxStorageOutDetailedParam hxStorageOutDetailedParam,String condition,String startTime,String endTime) {
        if (hxStorageOutDetailedParam != null && hxStorageOutDetailedParam.getGoodsNames() != null && hxStorageOutDetailedParam.getGoodsNames().length > 0) {
            for (int i = 0; i < hxStorageOutDetailedParam.getGoodsNames().length; i++) {
                if (hxStorageOutDetailedParam.getGoodsNames()[i] != null) { // 进一步检查数组元素是否为 null
                    hxStorageOutDetailedParam.getGoodsNames()[i] = hxStorageOutDetailedParam.getGoodsNames()[i].replace("& #40;", "(").replace("& #41;", ")");
                }
            }
        }
        User user = userService.getcurrentUser();
    	hxStorageOutDetailedParam.setDeptId(user.getDeptId());
    	hxStorageOutDetailedParam.setOperateId(user.getOperateId());
    	if(ToolUtil.isEmpty(hxStorageOutDetailedParam.getTimeType())) {
    		hxStorageOutDetailedParam.setTimeType("fj");
    	}
    	if (ToolUtil.isNotEmpty(startTime) && ToolUtil.isNotEmpty(endTime)) {
    		hxStorageOutDetailedParam.setTimeInterval("'"+startTime+"' and '"+endTime+"'");
		} else if (startTime == null && endTime == null){
			hxStorageOutDetailedParam.setTimeInterval("'"+DateUtil.getDate()+"' and '"+DateUtil.getDate()+"'");
		}
        return this.hxStorageOutDetailedService.findjiluPageBySpec(hxStorageOutDetailedParam,condition);
    }

    /**
     * 导出所有
     *
     * @author zhucx
     * @Date 2023-09-05
     */
    @ResponseBody
    @RequestMapping("/listAll")
    public List<HxStorageOutDetailedResult> listAll(HxStorageOutDetailedParam hxStorageOutDetailedParam,String condition,String startTime,String endTime) {
        if (hxStorageOutDetailedParam != null && hxStorageOutDetailedParam.getGoodsNames() != null && hxStorageOutDetailedParam.getGoodsNames().length > 0) {
            for (int i = 0; i < hxStorageOutDetailedParam.getGoodsNames().length; i++) {
                if (hxStorageOutDetailedParam.getGoodsNames()[i] != null) { // 进一步检查数组元素是否为 null
                    hxStorageOutDetailedParam.getGoodsNames()[i] = hxStorageOutDetailedParam.getGoodsNames()[i].replace("& #40;", "(").replace("& #41;", ")");
                }
            }
        }
        User user = userService.getcurrentUser();
    	hxStorageOutDetailedParam.setDeptId(user.getDeptId());
    	hxStorageOutDetailedParam.setOperateId(user.getOperateId());
    	if(ToolUtil.isEmpty(hxStorageOutDetailedParam.getTimeType())) {
    		hxStorageOutDetailedParam.setTimeType("fj");
    	}
    	if (ToolUtil.isNotEmpty(startTime) && ToolUtil.isNotEmpty(endTime)) {
    		hxStorageOutDetailedParam.setTimeInterval("'"+startTime+"' and '"+endTime+"'");
		} else if (startTime == null && endTime == null){
			hxStorageOutDetailedParam.setTimeInterval("'"+DateUtil.getDate()+"' and '"+DateUtil.getDate()+"'");
		}
        return this.hxStorageOutDetailedService.listAll(hxStorageOutDetailedParam,condition);
    }

    /**
     * 合计
     *
     * @author zhucx
     * @Date 2023-09-05
     */
    @ResponseBody
    @RequestMapping("/count")
    public HxStorageOutDetailedResult count(HxStorageOutDetailedParam hxStorageOutDetailedParam,String condition,String startTime,String endTime) {
        if (hxStorageOutDetailedParam != null && hxStorageOutDetailedParam.getGoodsNames() != null && hxStorageOutDetailedParam.getGoodsNames().length > 0) {
            for (int i = 0; i < hxStorageOutDetailedParam.getGoodsNames().length; i++) {
                if (hxStorageOutDetailedParam.getGoodsNames()[i] != null) { // 进一步检查数组元素是否为 null
                    hxStorageOutDetailedParam.getGoodsNames()[i] = hxStorageOutDetailedParam.getGoodsNames()[i].replace("& #40;", "(").replace("& #41;", ")");
                }
            }
        }
    	User user = userService.getcurrentUser();
    	hxStorageOutDetailedParam.setDeptId(user.getDeptId());
    	hxStorageOutDetailedParam.setOperateId(user.getOperateId());
    	if(ToolUtil.isEmpty(hxStorageOutDetailedParam.getTimeType())) {
    		hxStorageOutDetailedParam.setTimeType("fj");
    	}
    	if (ToolUtil.isNotEmpty(startTime) && ToolUtil.isNotEmpty(endTime)) {
    		hxStorageOutDetailedParam.setTimeInterval("'"+startTime+"' and '"+endTime+"'");
		} else if (startTime == null && endTime == null){
			hxStorageOutDetailedParam.setTimeInterval("'"+DateUtil.getDate()+"' and '"+DateUtil.getDate()+"'");
		}
        return this.hxStorageOutDetailedService.count(hxStorageOutDetailedParam,condition);
    }

    /**
     * 查询列表
     *
     * @author zhucx
     * @Date 2023-09-05
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(HxStorageOutDetailedParam hxStorageOutDetailedParam,String condition ) {
        return this.hxStorageOutDetailedService.findPageBySpec(hxStorageOutDetailedParam,condition);
    }

}


